package com.woniuxy.educationplatform.mapper;

import com.woniuxy.educationplatform.bean.ProjectBean;
import com.woniuxy.educationplatform.bean.SubjectBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;


@Mapper
public interface ProjectMapper {
    /**
     * 查询所有项目
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "projName", column = "proj_name", javaType = String.class),

    })
    @Select("select proj_name,id from project")
    List<ProjectBean> findAll();

    /**
     * 根据id查询项目(project查的是subject表，然后project_name用类似projectBean.name的形式取出)
     * @param id
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "projName", column = "proj_name", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
    })
    @Select("select * from project where id = #{id}")
    ProjectBean findById(Long id);

    /**
     * 新建项目，分两步，执行两句sql语句，放回p.id作为subject表的fk_project_id,
     * @param projectBean
     * @return
     */
    @Insert("insert into project (proj_name,create_time) values(#{p.projName},now())")
//    @SelectKey(keyProperty = "id",keyColumn = "id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
//    @Options(useGeneratedKeys=true,keyColumn="id")
    void saveProjectBean(@Param("p") ProjectBean projectBean);

    @Select("select max(id) from project")
    Long findMaxId();

    @Select("select id from project where proj_name = #{projName}")
    Long findIdByName(String projName);

}
